﻿package net.area80.sitemanager.display
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.BlurFilter;
	import gs.TweenLite;
	import net.area80.sitemanager.SiteManagerProxy;
	
	/**
	* BlurFreezeClip
	* @author Wissarut Pimanmassuriya
	*/
	public class  BlurFreezeClip extends FreezeClip
	{
		private var bmp:Bitmap;
		private var bmpData:BitmapData;
		private var colorClip:Sprite;
		private var colorContainer:Sprite;
		private var blur:int;
		private var bg:Bitmap;
		private var noBlurBmpData:BitmapData;
		
		public function BlurFreezeClip (color:int = 0x000000, alpha:Number = 0, blur:int = 10):void {
				super(color, alpha);
				this.blur = blur;
				colorClip = new Sprite();
				colorContainer = new Sprite();
				colorClip.addChild(colorContainer);
				addChild(colorClip);
		}
		public override function fadeUp ():void {
			colorClip.alpha = 0;
			TweenLite.to(colorClip, 0.5, { alpha:1 } );
		}
		public override function fadeDown ():void {
			TweenLite.to(colorClip, 0.5, { alpha:0 } );
		}
		protected override function create (e:Event):void {
			noBlurBmpData = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0x000000);
			var rootObj:DisplayObject;
			if (SiteManagerProxy.siteManager && SiteManagerProxy.siteManager.main && root == SiteManagerProxy.siteManager.main) {
				rootObj = root;
			} else {
				if (!root.parent.root) {
					rootObj = root;
				} else {
					rootObj = root.parent.root;
				}
			}
			if (!rootObj) return;
			noBlurBmpData.draw(rootObj);
			var myBmp:Bitmap = new Bitmap(noBlurBmpData);
			myBmp.filters = [new BlurFilter(blur, blur, 3)];
			bg = new Bitmap(noBlurBmpData);
			bmpData = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0x000000);
			bmpData.draw(myBmp);

			bmp = new Bitmap(bmpData);
			colorClip.addChildAt(bmp, 0);
			addChildAt(bg, 0);
			colorContainer.graphics.beginFill(freezeColor, freezeAlpha);
			colorContainer.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
		}
		protected override function dispose (e:Event):void {
				if (colorClip.contains(bmp)) colorClip.removeChild(bmp);
				if (contains(bg)) removeChild(bg);
				bmp = null
				if(bmpData) bmpData.dispose();
				if(noBlurBmpData) noBlurBmpData.dispose();
				bmpData = null;
				colorClip.graphics.clear();
		}
	}
	
}